package com.kepler.social.domain.user.account;

import com.kepler.social.common.constants.Enums;
import com.kepler.social.common.constants.TipsConst;
import com.kepler.social.common.utils.ValidateAssert;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Service
@Slf4j
public class UserAccountDomainService {

    @Resource
    private UserAccountRepository userAccountRepository;

    @Transactional(rollbackFor = Exception.class)
    public void createOfEmail(String email, Long userId) {
        if(!userAccountRepository.findByUserId(userId).isExist()){
            userAccountRepository.create(UserAccountFactory.createOfEmail(email, userId));
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void createOfMobilePhone(Integer countryCallingCode, Long mobilePhoneNumber, Long userId) {
        if(!userAccountRepository.findByUserId(userId).isExist()){
            userAccountRepository.create(UserAccountFactory.createOfMobilePhone(countryCallingCode, mobilePhoneNumber, userId));
        }
    }

    public void userFeatureEdit(String feature, String featureValue, Long userId) {
        Enums.UserFeatureEnum featureEnum = Enums.UserFeatureEnum.getByType(feature);
        ValidateAssert.notNull(featureEnum, TipsConst.user_feature_not_exists);
        userAccountRepository.updateSingleFiled(featureEnum.dbField, featureEnum.convert(featureValue), userId);
    }

}
